<?php
/**
* Site management.
*
* Copyright (c) 2009 James Gauld <james@jamesgauld.net>
* This file is part of Scribble.
* @license http://www.scribblecms.co.uk/license.txt
*
* @package Scribble
*/
use Buan\Config;
use Buan\Model;
use Buan\ModelCriteria;
use Buan\ModelManager;
use Buan\SystemLog;
class SiteController extends ScribbleAdminController {

	/**
	* Edit a site.
	*
	* @param array Action parameters
	* @param string Site ID
	* @return View
	*/
	private function edit($params, $siteId) {

		// Init
		$view = new ScribbleAdminView();
		if(!$this->init($view)) {
			return $view;
		}

		// Load site
		$site = Model::create('Site');
		$site->id = $siteId;
		if(!$site->getModelManager()->load($site)) {
			SystemLog::add('Cannot find a site matching the provided ID.', SystemLog::WARNING);
			return new ScribbleAdminView();
		}
		$view->site = $site;

		// Auth: Can the user modify this site
		if(!ScribbleAuth::authorize(ScribbleSession::getUser(), 'site-author', $site)) {
			SystemLog::add('Sorry, you do not have the authority to modify this site.', SystemLog::WARNING);
			return $view;
		}
		$view->setSource(Config::get('ext.Scribble.dir.views').'/scribble/site/edit.tpl.php');

		// Delete
		if(!empty($_POST) && isset($_POST['method']) && strtolower($_POST['method'])=='delete') {

			// Cannot remove current site
			$dSiteId = $site->id;
			$dPrefix = $site->prefix;
			if($dSiteId===SiteManager::getCurrentSite()->id) {
				SystemLog::add('Cannot remove the current site.', SystemLog::WARNING);
			}

			// Delete: Fail
			else if(!$site->getModelManager()->delete($site)) {
				SystemLog::add('Failed to delete Site.', SystemLog::WARNING);
			}

			// Delete: Success
			else {
				$user = ScribbleSession::getUser();
				LogManager::add("Site site:{$dSiteId}:{$dPrefix} was deleted by user:{$user->id}:{$user->displayname}", LogManager::USER_ACTION, $user->id);
			}
		}

		// Save
		else if(!empty($_POST)) {

			// Gather
			$privileges = ScribbleAuthWidget::gatherPrivilegesFromPost($site->id, isset($_POST['privileges']) ? $_POST['privileges'] : array());

			// Save: Fail
			if(!$site->getModelManager()->save($site)) {
				SystemLog::add('Failed to save site.', SystemLog::WARNING);
			}

			// Save: Success
			else {
				SystemLog::add('Site saved.', SystemLog::INFO);

				// Save privileges
				ScribbleAuthWidget::saveGatheredPrivileges($site->id, $privileges);

				// Log
				$user = ScribbleSession::getUser();
				LogManager::add("Site site:{$site->id}:{$site->prefix} was modified by user:{$user->id}:{$user->displayname}", LogManager::USER_ACTION, $user->id);
			}
		}

		// Generate the privilege data for use in the ui widget
		$view->privilegeData = ScribbleAuthWidget::getResourcePrivileges($site, 'site-edit');

		// Result
		return $view;
	}

	/**
	* Displays all sites.
	*
	* @param array Action parameters (none supported)
	* @return ScribbleAdminView
	*/
	public function index($params) {

		// Init
		$view = new ScribbleAdminView();
		if(!$this->init($view)) {
			return $view;
		}
		$view->setSource(Config::get('ext.Scribble.dir.views').'/scribble/site/index.tpl.php');

		// If there is no "current" site alert the user to this
		if(SiteManager::getCurrentSite()===NULL) {
			SystemLog::add('You have not defined a Site for the current domain. Please rectify this by altering the relevant site below.', SystemLog::WARNING);
		}

		// Prepare secondary view
		$sView = new ScribbleAdminView();
		$sView->setSource(Config::get('ext.Scribble.dir.views').'/scribble/site/index.secondary.tpl.php');
		ScribbleAdminView::getGlobalView()->attachViewToSlot($sView, 'secondary');

		// Load all sites
		$view->sites = ModelManager::select('Site');

		// Result
		return $view;
	}

	/**
	* Forwards through requests to the ::edit() method.
	*
	* @param array Action parameters
	* @param string Requested action. This is the Site ID.
	* @return View
	*/
	public function unknown($params, $action) {
		return $this->edit($params, $action);
	}
}
?>